package utils

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"path"
	"runtime"
	"strings"

	"github.com/go-musicfox/notificator"
	"github.com/pkg/errors"

	"gitee.com/nidielaila/go-mf/internal/configs"
	"gitee.com/nidielaila/go-mf/internal/types"
)


// TODO: not support MacOs notifier



type Notificator struct {
	// TODO: add field maybe
	*notificator.Notificator
}

func (noti Notificator) Push(urgency, title, text, iconPath, redirectUrl, groupId string) error {
	return n.Notificator.Push(urgency, title, text, iconPath, redirectUrl)
}

type NotifyContent struct {
	Title string
	Text string
	Url string
	Icon string
	GroupId string
}

func NewNotificator(noti notificator.Options) *Notificator {
	n := &Notificator{
		Notificator: notificator.New(o),
	}

	return n
}


func Notify(content NotifyContent) {
	if !configs.ConfigRegistry.Main.ShowNotify {
		return
	}

	nofify := NewNotificator(notificator.Options {
		AppName: types.AppName,
	})
	
	if runtime.GOOS != "darwin" {
		localDir := GetLocalDataDir()
		content.Icon = path.Join(localDir, configs.ConfigRegistry.Main.NotifyIcon)
		if _, err := os.Stat(content.Icon); os.IsNotExist(err) {
			content.Icon = path.Join(localDir, types.DefaultNotifyIcon)
			// 写入logo.png 文件
			err = CopyFileFromEmbed("embed_config/logo.png", content.Icon)

			if err != nil {
				log.Printf("copy logo.png failed, err: %+v", errors.WithStack(err))
			}
		} else if err != nil {
			log.Printf("logo.png status err: %+v", errors.WithStack(err))
		}
	}

	_ = nofify.Push(notificator.UrNormal, content.Title, content.Text, content.Url, content.GroupId)
}